package com.lbmb.photodrawaimanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbmb.photodrawaimanager.entity.dto.PhotoUserDownLoadEntity;
import com.lbmb.photodrawaimanager.entity.resp.PhotoUserDownLoadResp;
import com.lbmb.photodrawaimanager.entity.resp.PhotoUserUploadResp;
import com.lbmb.photodrawaimanager.mapper.PhotoUserDownLoadMapper;
import com.lbmb.photodrawaimanager.service.HuoShanService;
import com.lbmb.photodrawaimanager.service.PhotoUserDownLoadService;
import com.lbmb.photodrawaimanager.service.PhotoUserUploadService;
import com.lbmb.photodrawaimanager.utils.FileOpeationUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author hanWang
 * @Title:
 * @Package
 * @Description:
 * @date 2023/8/64:15 上午
 */
@Service
public class PhotoUserDownLoadServiceImpl extends ServiceImpl<PhotoUserDownLoadMapper, PhotoUserDownLoadEntity> implements PhotoUserDownLoadService {
  @Autowired
  private PhotoUserDownLoadMapper photoUserDownLoadMapper;
  @Autowired
  private HuoShanService  huoShanService;
  @Autowired
  private PhotoUserUploadService photoUserUploadService;

    @Override
    public Long insert(Long userUploadId, Long sourceId) {
        PhotoUserDownLoadEntity entity = new PhotoUserDownLoadEntity();
        entity.setIsSave(0);
        entity.setUserUploadId(1L);
        entity.setVideoId(sourceId);
        entity.setUserUploadId(userUploadId);
        photoUserDownLoadMapper.insert(entity);
        return entity.getId();
    }

    @Override
    @Async
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Long uploadById(Long id, Long sourceId,String fileUrl) {
        String huafengFilePath = huoShanService.downloadManHuaFengImg(true, "", fileUrl);
        LambdaUpdateWrapper<PhotoUserDownLoadEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(PhotoUserDownLoadEntity::getId,id);
        PhotoUserDownLoadEntity entity = new PhotoUserDownLoadEntity();
        entity.setImgUrl(huafengFilePath);
        photoUserDownLoadMapper.update(entity,wrapper);
        return null;
    }

    @Override
    public PhotoUserDownLoadResp makeImgOperation(Long uploadId,String carToonType) {
        Long userId = 1L;
        PhotoUserUploadResp photoUserUploadResp = photoUserUploadService.selectById(uploadId);
        String imgBase = photoUserUploadResp.getImgBase();
        String imgUrl = photoUserUploadResp.getImgUrl();
        String huafengFilePath = huoShanService.downloadManHuaFengImg(true, carToonType, imgUrl);
        String base64 = FileOpeationUtils.getBase64(huafengFilePath);
        PhotoUserDownLoadEntity entity = new PhotoUserDownLoadEntity();
        // 优化 取决于文件类型
        entity.setType(1);
        entity.setUserId(userId);
        //entity.setImgBase(base64);
        entity.setImgUrl(huafengFilePath);

        entity.setVideoId(photoUserUploadResp.getVideoId());
        entity.setUserUploadId(photoUserUploadResp.getId());
        entity.setIsSave(1);
        photoUserDownLoadMapper.insert(entity);
        PhotoUserDownLoadResp photoUserDownLoadResp = new PhotoUserDownLoadResp();
        BeanUtils.copyProperties(entity,photoUserDownLoadResp);
        return photoUserDownLoadResp;
    }


    @Override
    public PhotoUserDownLoadResp queryByUpLoadIdOrId(Long uploadId, Long id) {
        Long userId = 1L;
        LambdaQueryWrapper<PhotoUserDownLoadEntity> wrapper = new LambdaQueryWrapper<>();
        if (uploadId!=null){
            wrapper.eq(PhotoUserDownLoadEntity::getUserUploadId,uploadId);
        }
        if (id!=null){
            wrapper.eq(PhotoUserDownLoadEntity::getId,id);
        }
        wrapper.eq(PhotoUserDownLoadEntity::getUserId,userId);
        PhotoUserDownLoadEntity entity = photoUserDownLoadMapper.selectOne(wrapper);
        PhotoUserDownLoadResp photoUserDownLoadResp = new PhotoUserDownLoadResp();
        BeanUtils.copyProperties(entity,photoUserDownLoadResp);
        return photoUserDownLoadResp;
    }
}
